#include "MIDIProcessor.h"
#include "RtMidi.h"


MIDIProcessor::MIDIProcessor(void)
{
	midiout = new RtMidiOut();
	midiout->openPort(0);

	for(int i = 0; i < 128; i++)
		notes[i] = false;
}


MIDIProcessor::~MIDIProcessor(void)
{
	if(midiout != 0)
		delete midiout;
}

void MIDIProcessor::changeInstrument(int instrument)
{
	message.clear();
	message.push_back(192);
	message.push_back(instrument);
	midiout->sendMessage(&message);
}

void MIDIProcessor::noteOn(int note, int velocity)
{
	message.clear();
	message.push_back(144);
	message.push_back(note);
	message.push_back(velocity);
	midiout->sendMessage(&message);

	notes[note] = true;
}

void MIDIProcessor::noteOff(int note)
{
	message.clear();
	message.push_back(128);
	message.push_back(note);
	message.push_back(0);
	midiout->sendMessage(&message);

	notes[note] = false;
}

void MIDIProcessor::setVolume(int volume)
{
	message.clear();
	message.push_back(176);
	message.push_back(7);
	message.push_back(volume);
	midiout->sendMessage(&message);
}

void MIDIProcessor::stopAll()
{
	for(int i = 0; i < 128; i++)
	{
		if(notes[i])
			noteOff(i);
	}
}

void MIDIProcessor::stopAllExcept(int n)
{
	for(int i = 0; i < 128; i++)
	{
		if(i == n)
			continue;

		if(notes[i])
			noteOff(i);
	}
}

